import { create, update, remove, query } from '../../framework/service/index';
import { routerRedux } from 'dva/router';
import { ToastTips } from '../../common/toastTips/tosat';

export default {

  namespace: 'relations',

  state: {
    validOneToOneData:{},
    validOneToOneStatus:false,
    limitPost:false,
    typeArr:[{"cover":"","id":"0","name":"全部类型","pid":""}],
    areasArr:[{"id":"0","name":"区域","pid":"0","type":"0"}],
    cityName:''
  },

  subscriptions: {
    setup({ dispatch, history }) {

    },
  },

  effects: {

    //提交 经纬度
    *postLatAndLng({ payload }, { call, put }) {

      const latitude = window.MC.latitude ? window.MC.latitude : 0;
      const longitude = window.MC.longitude ? window.MC.longitude : 0;
      // console.log('post 经纬度');
      if(latitude && longitude){
        const { code, data, message } = yield call(create, '/api/applicant/geoLaction', {latitude,longitude});
        // console.log('code', code);
        // console.log('data', data);
        // console.log('message', message);
        if (code === 200) {
          // ToastTips(message, 2000, null);
        }else{
          // ToastTips(message, 2000, null);
        }
      }else{
        console.log('获取经纬度失败');
      }

    },

    //获取全部兼职页---职业类型列表
    *getTypes({ payload }, { call, put, select }) {

      const { code, data, message } = yield call(query, '/api/pub/jobs/types/all', {});
      // console.log('全部兼职 全部类型 data', JSON.stringify(data));
      if (code === 200 && data) {

        let typeArr = yield select (({relations}) => relations.typeArr);
        if(typeArr && typeArr.length == 1){
          data.map((item, index) =>{
            typeArr.push(item);
          })
          yield put({
            type: 'querySuccess',
            payload: {
              typeArr
            }
          })
        }
      }

    },

    //获取全部兼职页---地区列表
    *getAreas({ payload }, { call, put, select }) {

      const { cityName } = yield select (({relations}) => relations);
      // console.log('cityName relations',cityName);
      let cName = cityName;
      if(!cName){
        cName = window.MC.cityName;
      }

      if(cName){
        const { code, data, message } = yield call(query, `/api/pub/areas/${cName}`, {});
        // console.log('全部兼职 地区列表 data', JSON.stringify(data));
        if (code === 200 && data) {

          let areasArr = [{"id":"0","name":"区域","pid":"0","type":"0"}];
          data.map((item, index) =>{
            areasArr.push(item);
          })
          // console.log('areasArr', JSON.stringify(areasArr));
          yield put({
            type: 'querySuccess',
            payload: {
              areasArr
            }
          })
        }
      }else{
        console.log('获取城市区域信息失败');
      }

    },
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
